﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
 
using BOHVote.BLL;
using BOHVote.Common;
using BOHVote.Entity;

namespace BOH.ControlPanel
{
    public partial class UserAdd : System.Web.UI.Page
    {
        protected string PageType = string.Empty;
        protected string UserID = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        { 
           PageType = Request.QueryString["type"];
           UserID = Request.QueryString["ID"];
            if (!IsPostBack)
            {
               
                BindGovern();
                if (PageType != null && UserID!=null&&UserID.Trim().Length > 0 && PageType == "edit")
                {
                    BindFieldToFrom(UserID.Trim());
                }
            }
        }

        public void BindFieldToFrom(string UserID)
        {
            BHUser bu = UserClass.getUserByID(UserID);
            this.txtUserName.Text=bu.UserName  ;
             //this.txtPassword.Text =bu.password;
             hidPassword.Value = bu.password;
             this.txtUserCode.Text = bu.UserCode;
             this.txtMail.Text = bu.Mail;
             //this.drpUserType.SelectedValue = bu.UserType;
             this.drpG.SelectedValue = bu.GovernID;
             drpG_SelectedIndexChanged(null, null);
             this.drpU.SelectedValue = bu.UnitID;
             this.txtPhone.Text = bu.Phone;
             this.drpUserStatus.SelectedValue = bu.Status;
        }

        public void BindGovern()
        {
            DataSet dstGovern = UnitClass.getGovern();
            drpG.DataSource = dstGovern.Tables[0].DefaultView;
            drpG.DataTextField = "GovernName";
            drpG.DataValueField = "GovernID";
            drpG.DataBind();
            ListItem listitem = new ListItem("", "");
            listitem.Selected = true;
            drpG.Items.Add(listitem);
        }

        public void BindUnit(string GovernID)
        { 
            DataSet dst = UnitClass.getUnitByGovern(GovernID);
            drpU.DataSource = dst;
            drpU.DataTextField = "UnitName";
            drpU.DataValueField = "UnitID";
            drpU.DataBind();
            ListItem listitem = new ListItem("", "");
            listitem.Selected = true;
            drpU.Items.Add(listitem);
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            BHUser bu = new BHUser();
            bu.UserCode = this.txtUserCode.Text;
            bu.UserName = this.txtUserName.Text.ToString();
            //FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text.Trim(), "MD5");
            bu.Mail = this.txtMail.Text.ToString();
            //bu.UserType = this.drpUserType.SelectedValue.ToString();
            if (!String.IsNullOrEmpty(txtPassword.Text))
            {
                bu.password = txtPassword.Text.Trim();
            }
            else if (!String.IsNullOrEmpty(hidPassword.Value))
            {
                bu.password = hidPassword.Value;
            }
            else
            {
                bu.password = common.RandomPassword();
            }
            if(!String.IsNullOrEmpty(this.drpG.SelectedValue.ToString()))
                bu.GovernID = this.drpG.SelectedValue.ToString();
            if (!String.IsNullOrEmpty(this.drpU.SelectedValue.ToString()))
                bu.UnitID = this.drpU.SelectedValue.ToString();
            bu.Phone = this.txtPhone.Text.ToString();
            bu.Status = this.drpUserStatus.SelectedValue.ToString();
            if (PageType != null && UserID != null && UserID.Trim().Length > 0 && PageType == "edit")
            {
                bu.UserID = UserID;
                UserClass.UpdateUser(bu);
            }
            else
            {
                UserClass.InsertUser(bu);
            }

            this.ClientScript.RegisterClientScriptBlock(this.Page.GetType(),"alert", "<script>SaveSuccess();</script>");
        }

        protected void drpG_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindUnit(drpG.SelectedValue.ToString());
        }


    }
}
